package org.lq.ssm.service.impl;

import org.apache.ibatis.annotations.Mapper;
import org.lq.ssm.dto.MenuDTO;
import org.lq.ssm.dto.mapper.MenuDTOMapper;
import org.lq.ssm.mapper.MenuPOMapper;
import org.lq.ssm.po.MenuPO;
import org.lq.ssm.po.MenuPOExample;
import org.lq.ssm.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuDTOMapper menuDTOMapper;
    @Autowired
    MenuPOMapper menuPOMapper;

    @Override
    public List<MenuDTO> getAllMenu(Long pid) {
        MenuPOExample menuPOExample = new MenuPOExample();
        MenuPOExample.Criteria criteria = menuPOExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<MenuPO> menuPOS = menuPOMapper.selectByExample(menuPOExample);
        List<MenuDTO> menuDTOS = menuDTOMapper.menuPOSToMenuDTOS(menuPOS);
        if (menuPOS==null||menuPOS.size()==0){return null;}
        else{
            for (MenuDTO menuDTO : menuDTOS){
                menuDTO.setChildren(getAllMenu(menuDTO.getId()));
            }
        }
        return menuDTOS;
    }
}
